package com.example.administrator.androidros.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;

import com.example.administrator.androidros.R;
import com.example.administrator.androidros.base.BaseActivity;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

public class ScanActivity extends BaseActivity implements QRCodeView.Delegate {
    @BindView(R.id.activity_scan_zxingview)
    protected ZXingView activity_scan_zxingview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_scan);
    }

    @Override
    protected void initData() {
        initCommonHeader();
        //设置二维码的代理
        activity_scan_zxingview.setDelegate(this);
    }

    @Override
    protected void onFailure(int requestCode, Object object) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object) {

    }

    //扫描成功解析二维码成功后调用
    @Override
    public void onScanQRCodeSuccess(String result) {
        //扫描成功后调用震动器
        vibrator();
        Intent intent = new Intent();
        intent.putExtra("result", result);
        setResult(RESULT_OK, intent);
        finish();
        //再次延时1.5秒后启动
//        activity_scan_zxingview.startSpot();
    }

    //扫描失败后调用的方法
    @Override
    public void onScanQRCodeOpenCameraError() {
        showToast(getString(R.string.common_camera_open_failure));
    }

    @Override
    protected void onStart() {
        super.onStart();
        //打开后置摄像头预览,但并没有开始扫描
        activity_scan_zxingview.startCamera();
        //开启扫描框
        activity_scan_zxingview.showScanRect();
        activity_scan_zxingview.startSpot();
    }

    @Override
    public void onStop() {
        activity_scan_zxingview.stopCamera();
        super.onStop();
    }

    @Override
    public void onDestroy() {
        activity_scan_zxingview.onDestroy();
        super.onDestroy();
    }

    private void vibrator() {
        //获取系统震动服务
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    protected void initCommonHeader() {
        configCommonHeader(this, getString(R.string.device_add), false);
    }
}
